package utl.mofen.delphi.cds;

import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;

import utl.mofen.delphi.cds.convert.Column;
import utl.mofen.utils.Base64;

public class Util {
	
	public static String Escap4Xml(String data)
	{
		String ret=data.replaceAll("\"", "&quot;");
		ret = ret.replaceAll("<", "&lt;");
		ret = ret.replaceAll(">", "&gt;");
		return ret;
	}
	
	/**
	 * 将整个数据集转换成Delphi中ClientDataSet的XMLData
	 * @param rs
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public static String getXMLData(ResultSet rs) throws Exception,
			IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" standalone=\"yes\"?>");
		sb.append("<DATAPACKET Version=\"2.0\">\n");

		ResultSetMetaData rsmd = rs.getMetaData();
		sb.append("<METADATA>\n");
		sb.append("<FIELDS>\n");

		for (int i = 0; i < rsmd.getColumnCount(); i++) {
			sb.append(Column.FieldInfo(rsmd, i + 1) + "\n");
		};

		sb.append("</FIELDS>\n");
		sb.append("<PARAMS/>\n");
		sb.append("</METADATA>\n");

		sb.append("<ROWDATA>\n");

		while (rs.next()) {
			// <ROW FKey="{27C94F8F-4B6B-4CE3-B4D9-4FFF7ABD2F95}"
			// FCode="240012104" FName="T0005" FDisCount="10"
			// FBkindKey="{068697C8-C886-45D6-86BB-37F808A868D1}"
			// FBKindCode="24" FBKillName="妫夋湇"
			// FSKindKey="{6ED85D48-492A-41A3-8808-8EB929078A8E}"
			// FSKindCode="21" FSKindName="澶栬锤鍘熷崟鍗。"
			// FBrandKey="{D5B9423F-9A56-4843-9894-7A479A426CF8}"
			// FBrandCode="001" FBrandName="閲囪僵" FKind="101"
			// FUpbuilderIP="192.168.1.11" FUpbuilder="瓒呯骇鐢ㄦ埛"
			// FUpbuildTime="20120131T09:42:40000" FUpdateVer="AAAAAAAFbvE="/>
			
			sb.append("<ROW \n");
			for (int i = 0; i < rsmd.getColumnCount(); i++) 
			{
				int fieldType= rsmd.getColumnType(i + 1);
				String cName = rsmd.getColumnName(i+1);
				if(cName.equals(""))
				{
					cName = "column" + (i+1);
				}
				
				if (rs.getString(i + 1) != null) {
					if ((fieldType == Types.LONGVARBINARY) // text
							|| (fieldType == Types.BINARY) //timestamp,binary
							|| (fieldType == Types.BLOB)   // image
					) {
						byte[] buf = rs.getBytes(i + 1);
						String encodedString = Base64.encodeBytes(buf,
								Base64.NO_OPTIONS);
						sb.append(cName + " =\""
								+ encodedString + "\"\n");	
					} else if (rsmd.getColumnType(i + 1) == Types.TIMESTAMP)  //datetime 
					{					
	
						Timestamp dt = rs.getTimestamp(i+1);						
						SimpleDateFormat datef = new SimpleDateFormat("yyyyMMdd"); //格式化当前系统日期
						SimpleDateFormat timef = new SimpleDateFormat("HH:mm:ssms"); //格式化当前系统日期
					    String dtValue = datef.format(dt) + "T" +timef.format(dt);

						
						sb.append(cName + " =\""
									+ dtValue + "\"\n");
					} else {
						sb.append(cName + " =\""
								+ Escap4Xml(rs.getString(i + 1)) + "\"\n");
						
					}
				}
			}
			sb.append("/>\n");

			// sb.append("<ROW/>\n");
		}

		sb.append("</ROWDATA>\n");
		
		//System.out.println(sb.toString());

		return sb.toString();
	}
}
